//
//  LZSPwdViewController.m
//  IOSProject
//
//  Created by lzs on 2018/4/18.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSPwdViewController.h"

@interface LZSPwdViewController ()<UITextFieldDelegate>
@property (strong, nonatomic)   UITextField *userNameText;              /** 手机号text */
@property (strong, nonatomic)   UITextField *passwordText;              /** 密码text */
@property (strong, nonatomic)   UITextField *passOldwordText;
@property (strong, nonatomic)   UIButton *LoginBtn;              /** 登录button */
//@property (strong, nonatomic)   UIButton *goLandingBtn;              /** 去注册 */
@property (strong, nonatomic)   UILabel *textBackLabel;              /** 输入框背景图 */
@property (strong, nonatomic)   UILabel *textLineLabel;              /** text中间的分割线 */
@property (strong, nonatomic)   UILabel *textLineLabel1;              /** text中间的分割线 */
@end

@implementation LZSPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改密码";
    self.edgesForExtendedLayout = 0;
    
    [self.view addSubview:self.textBackLabel];
    [self.view addSubview:self.userNameText];
    [self.view addSubview:self.passwordText];
    [self.view addSubview:self.textLineLabel];
    [self.view addSubview:self.passOldwordText];
    [self.view addSubview:self.textLineLabel1];
    [self.view addSubview:self.LoginBtn];
//    [self.view addSubview:self.goLandingBtn];
//    //
//    [self.view addSubview:self.pwdBtn];
    
    [self addcontrainst];
}

-(void)addcontrainst{
    __weak typeof (self) weakSelf = self;
    
    [_textBackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@134);
        make.top.equalTo(weakSelf.view.mas_top).offset(15);
        make.left.equalTo(weakSelf.view.mas_left).offset(-1);
        make.right.equalTo(weakSelf.view.mas_right).offset(1);
    }];
    
    [_userNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.textBackLabel.mas_top);
    }];
    
    [_textLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.textBackLabel.mas_left).offset(15);
        make.right.equalTo(weakSelf.textBackLabel.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.userNameText.mas_bottom);
    }];
    
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.userNameText.mas_bottom).offset(1);
    }];

    [_textLineLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.textBackLabel.mas_left).offset(15);
        make.right.equalTo(weakSelf.textBackLabel.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.passwordText.mas_bottom);
    }];

    [_passOldwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(1);
    }];


    [_LoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left).offset(16);
        make.right.equalTo(weakSelf.view.mas_right).offset(-16);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.passOldwordText.mas_bottom).offset(15);
    }];
//
//    [_goLandingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(70, 16));
//        make.right.equalTo(weakSelf.view.mas_right).offset(-15);
//        make.top.equalTo(weakSelf.LoginBtn.mas_bottom).offset(23);
//    }];
//
//    [_pwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 16));
//        make.left.equalTo(weakSelf.view.mas_left).offset(15);
//        make.top.equalTo(weakSelf.LoginBtn.mas_bottom).offset(23);
//    }];
}

- (UILabel *)textBackLabel{
    if (!_textBackLabel) {
        _textBackLabel = [[UILabel alloc]init];
        _textBackLabel.backgroundColor = [UIColor whiteColor];
        _textBackLabel.layer.borderWidth = 1;
        _textBackLabel.layer.borderColor = RGB(188, 188, 188).CGColor;
    }
    return _textBackLabel;
}

- (UITextField *)userNameText{
    if (!_userNameText) {
        _userNameText = [[UITextField alloc]init];
        _userNameText.delegate = self;
        _userNameText.placeholder = @"请输入手机号码";
        [_userNameText addTarget:self action:@selector(userNameTextChangeText:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _userNameText;
}
- (UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"请输入旧密码";
        _passwordText.secureTextEntry = YES;
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _passwordText;
}

- (UITextField *)passOldwordText{
    if (!_passOldwordText) {
        _passOldwordText = [[UITextField alloc]init];
        _passOldwordText.delegate = self;
        _passOldwordText.placeholder = @"请输入新密码";
        _passOldwordText.secureTextEntry = YES;
        [_passOldwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _passOldwordText;
}

- (UILabel *)textLineLabel{
    if (!_textLineLabel) {
        _textLineLabel = [[UILabel alloc]init];
        _textLineLabel.backgroundColor = RGB(188, 188, 188);
    }
    return _textLineLabel;
}

- (UILabel *)textLineLabel1{
    if (!_textLineLabel1) {
        _textLineLabel1 = [[UILabel alloc]init];
        _textLineLabel1.backgroundColor = RGB(188, 188, 188);
    }
    return _textLineLabel1;
}


- (UIButton *)LoginBtn{
    if (!_LoginBtn) {
        _LoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_LoginBtn setTitle:@"修改密码" forState:(UIControlStateNormal)];
        _LoginBtn.backgroundColor = RGB(229, 229, 229);
        _LoginBtn.userInteractionEnabled = NO;
        [_LoginBtn setTitleColor:RGB(132, 132, 132) forState:(UIControlStateNormal)];
        [_LoginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        _LoginBtn.selected = NO;
        [_LoginBtn addTarget:self action:@selector(pushNextViewController) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _LoginBtn;
}

-(void)pushNextViewController{
    
   
    
    if (self.userNameText.text.length<11) {
        [self.view makeToast:@"手机号码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
//    //
//    //从注册那里获得数据
//    //根据用户名保存用户数据
//    NSDictionary *dics = [[NSUserDefaults standardUserDefaults] objectForKey:self.userNameText.text];
//    NSString *passwordstr = dics[@"password"];
    
    if (self.passwordText.text.length<6) {
        [self.view makeToast:@"旧密码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
    if (self.passwordText.text.length<6) {
        [self.view makeToast:@"旧密码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
    if (self.passOldwordText.text.length<6) {
        [self.view makeToast:@"新密码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
    if ([self.passwordText.text isEqualToString:self.passOldwordText.text]) {
        [self.view makeToast:@"新旧密码一样" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"key"] = @"cp";
    params[@"action"] = @"editpsw";
    
    params[@"oldpsw"] = self.passwordText.text;
    params[@"newpsw"] = self.passOldwordText.text;
    params[@"tel"]  = self.userNameText.text;
    
    [self postData:@"http://guleiba.com/api/caipiao/user/" param:params success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] ==1) {
            [self showTostMessage:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self showTostMessage:responseObject[@"msg"]];
        }
    } error:^(NSError *error) {
        [self showTostMessage:error.domain];
    }];


    
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.userNameText && range.location > 10) {
        return NO;
    }
    return YES;
}
#pragma mark -textChange
//用户名修改内容时调用的方法
- (void)userNameTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length == 11 && _passwordText.text.length > 5) {
        _LoginBtn.backgroundColor = RGB(56, 166, 241);
        _LoginBtn.userInteractionEnabled = YES;
        _LoginBtn.selected = YES;
    }else{
        _LoginBtn.backgroundColor = RGB(229, 229, 229);
        _LoginBtn.userInteractionEnabled = NO;
        _LoginBtn.selected = NO;
    }
}
//密码输入框修改内容是调用的方法
- (void)passwordTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length > 5 && _userNameText.text.length == 11) {
        _LoginBtn.backgroundColor = RGB(56, 166, 241);
        _LoginBtn.userInteractionEnabled = YES;
    }else{
        _LoginBtn.backgroundColor = RGB(229, 229, 229);
        _LoginBtn.userInteractionEnabled = NO;
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
